﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Runtime;

namespace TeighaHelper.Core
{
    public class CadDbReader
    {

        public static List<CADGeometryArc> listArcs = new List<CADGeometryArc>();
        public static List<CADGeometryLine> listLines = new List<CADGeometryLine>();
        public static List<CADGeometryPolyLine> listPolylines = new List<CADGeometryPolyLine>();
        public static List<CADMTextModel> listMTexts = new List<CADMTextModel>();
        public static List<CADDbTextModel> listDbTexts = new List<CADDbTextModel>();

        public static List<List<string[]>> xDatass = new List<List<string[]>>();
        // public static List< List<string[]>> efe = new List< List<string[]>>();  
        /// <summary>
        /// 取得CAD里的线，包括直线、多段线、圆曲线
        /// </summary>
        /// <param name="dwgFile"></param>
        /// <returns></returns>
        public static void GetCadData(string dwgFile)
        { 
            using (new Services())
            {
                using (Database database = new Database(false, false))
                {
                    database.ReadDwgFile(dwgFile, FileShare.Read, true, "");
                    using (var trans = database.TransactionManager.StartTransaction())
                    {
                        using (BlockTable table = (BlockTable)database.BlockTableId.GetObject(OpenMode.ForRead))
                        {
                            using (SymbolTableEnumerator enumerator = table.GetEnumerator())
                            {
                                StringBuilder sb = new StringBuilder();
                                while (enumerator.MoveNext())
                                {
                                    using (BlockTableRecord record = (BlockTableRecord)enumerator.Current.GetObject(OpenMode.ForRead))
                                    {

                                        foreach (ObjectId id in record)
                                        {
                                            Entity entity = (Entity)id.GetObject(OpenMode.ForRead, false, false);
                                            // CADGeometryModel geoModel = new CADGeometryModel();
                                            xDatass.Add( EntityReader.GetXData(entity));
                                            switch (entity.GetRXClass().Name)
                                            { 
                                                case "AcDbPolyline":
                                                    var geoPoly = EntityReader.polyLineFromEntity(entity);
                                                    listPolylines.Add(geoPoly);
                                                    break;
                                                case "AcDbLine":
                                                    var geoLine = EntityReader.lineFromEntity(entity);
                                                    listLines.Add(geoLine);
                                                    break;
                                                case "AcDbArc":
                                                    var geoArc = EntityReader.arcFromEntity(entity);
                                                    listArcs.Add(geoArc);
                                                    break;
                                                case "AcDbText":
                                                    var dbText = EntityReader.dbTextFromEntity(entity);
                                                    listDbTexts.Add(dbText);
                                                    break;
                                                case "AcDbMText":
                                                    var mText = EntityReader.mTextFromEntity(entity);
                                                    listMTexts.Add(mText);
                                                    break;
                                                default: 
                                                    break;
                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }
            //var sfe = efe.Where(e=>e.Count()!=0).ToList();
           // return (listArcs, listLines, listPolylines);
        }

    }
}
